package datastore.db;

import datastore.db.exception.NotSavedError;

public class Model {
    private static final long serialVersionUID = 8518914041036261469L;
    
    protected Model parent;
    protected transient Key key;
    protected transient String keyName;
    
    public Model() {
    }
    
    public Model(Model parent) {
        this.parent = parent;
    }
    
    public Model(String keyName) {
        this.keyName = keyName;
    }

    public Model(Model parent, String keyName) {
        this.parent = parent;
        this.keyName = keyName;
    }
    
    public String getKeyName() {
        return keyName;
    }
    
    public Model getParent() {
        return parent;
    }
    
    //TODO: getParentKey needed?
    public Key getParentKey() {
        if (parent == null) {
            return null;
        }
        return parent.getKey();
    }
    /* 
     * Model instances have the following methods:

key()

    Returns the datastore Key for this model instance.
    A model instance does not have a key until it is put() in the datastore. Calling key() before the instance has a key raises a NotSavedError.
*/
    public Key getKey() {
        if (!isSaved()) {
            throw new NotSavedError();
        }
        return key;
    }
    
    //TODO: setKey needed?
    public void setKey(Key key) {
        this.key = key;
    }
/*put()

    Stores the model instance in the datastore. If the model instance is newly created and has never been stored, this method creates a new data entity in the datastore. Otherwise, it updates the data entity with the current property values.

    The method returns the Key of the stored entity.*/
    public Key put() {
        key = Datastore.put(this);
        return key;
    }
/*delete()

    Deletes the model instance from the datastore. If the instance has never been put(), the delete does nothing.
     */
    public void delete() {
        if (isSaved()) {
            Datastore.delete(this);
        }
    }
    /*
is_saved()

    Returns True if the model instance has been put() into the datastore at least once.

    This method only checks that the instance has been stored at least once since it was created. It does not check if the instance's properties have been updated since the last time it was put().
    */
    public boolean isSaved() {
        return key != null;
    }

    /*dynamic_properties()

    Returns a list of the names of all of the dynamic properties defined for this model instance. This only applies to instances of Expando classes. For non-Expando model instances, this returns an empty list.
parent()

    Returns a model instance for the parent entity of this instance, or None if this instance does not have a parent.
parent_key()

    Returns the Key of the parent entity of this instance, or None if this instance does not have a parent.
to_xml()

    Returns an XML representation of the model instance.

    Property values conform to the Atom and GData specifications.

     */
}
